package com.wl.dzt.ide.controller;

import com.wl.dzt.ide.service.EntityGeneratorService;
import com.wl.dzt.sys.controller.param.Param;
import com.wl.dzt.sys.core.dao.datachange.DataChangeter;
import com.wl.dzt.sys.core.dao.db.DBColumn;
import com.wl.dzt.sys.ui.ext.ExtTemplate;
import com.wl.dzt.sys.util.WebUtils;
import com.wl.dzt.sys.util.json.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 实体生成控制器
 *
 */
@Api(description = "admin/ide", tags = "开发工具(basic-api)")
@Controller
@RequestMapping("admin/ide")
public class EntityGeneratorController {

    /**
     * 实体生成服务层
     */
    @Resource
    private EntityGeneratorService entityGeneratorService;

    /**
     * 查询所有表格名称
     *
     * @param response
     * 			 响应
     * @throws SQLException
     * 			 异常
     */
    @ApiOperation("查询所有表格名称")
    @PostMapping("allTable")
    public void allTable(@ApiIgnore HttpServletResponse response) throws SQLException {
        DataChangeter changeter = new DataChangeter() {
            public List<Map<String, Object>> change(List<Map<String, Object>> data) {
                data.forEach(map -> {
                    map.put("iconCls", "x-fa-database");
                    map.put("checked", false);
                    List<Map<String, Object>> children = (List<Map<String, Object>>) map.get("children");
                    if (children.size() >= 1) {
                        change(children);
                    }
                });
                return data;
            }
        };
        WebUtils.print(Json.parse(changeter.change(entityGeneratorService.allTable())), response);
    }

    /**
     * 根据列名查询字段
     *
     * @param request
     *            请求
     * @param response
     *            响应
     * @throws SQLException
     *             数据异常
     */
    @ApiOperation("根据列明查询字段")
    @PostMapping("getCols")
    public void getCols(@ApiIgnore HttpServletRequest request, @ApiIgnore HttpServletResponse response) throws SQLException {
        String tableName = "";
        Param param = WebUtils.getParam(request);
        if (!param.getTerms().isEmpty()) {
            tableName = param.getTerms().get(0).getValue().toString();
            List<DBColumn> list = entityGeneratorService.allCols(tableName);
            ExtTemplate.gridTemplate(list, list.size(), response);
        }
    }

    /**
     *
     * 创建实体
     *
     * @param request 请求
     * @param response 响应
     * @param tbs 表名
     * @param pack 包名
     * @throws Exception 异常
     */
    @ApiOperation("创建实体")
    @PostMapping("createEntity")
    public void createEntity(@ApiIgnore HttpServletRequest request, @ApiIgnore HttpServletResponse response, String tbs, String pack) throws Exception {
        entityGeneratorService.createEntity(tbs.split(","), pack);
    }
}
